package com.study.businessone

import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.module.compose.base.ComposeActivity
import com.module.compose.base.composable.CommonPage
import com.module.compose.base.utils.ToastUtils
import com.module.compose.base.viewModel.OurViewModel
import com.study.messagebus.core.MessageBus
import com.study.messagebus.Receive
import com.study.messagebus.Register
import com.study.zrouter.ZRouter
import com.zrouter.annotation.Route

@Register
@Route("businessOne/BusinessOneActivity")
class BusinessOneActivity : ComposeActivity<OurViewModel>() {

    @Receive("businessOne/BusinessOneActivity")
    fun receiveMsg(msg: Map<String, String>) {
        msg["key"]?.let {
            ToastUtils.show(it)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        MessageBus.send(
            //消息标签，需与接收器进行匹配
            "main/MainActivity",
            //消息内容，必须为Map<String,String>类型
            mapOf("key" to "从另一个组件过来的MessageBus消息"),
            //true为即时消息，false只有当接收器的页面处于前台才接收消息
            true
        )
    }

    @Composable
    override fun ContentView() {
        CommonPage(
            title = "业务组件",
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {

            Button(onClick = {
                ZRouter.instance().routeTo(
                    this@BusinessOneActivity,
                    "main/MainActivity",
                    null
                )
            }) {
                Text(text = "跳转到主页")
            }

            Button(onClick = {
                MessageBus.send(
                    //消息标签，需与接收器进行匹配
                    "main/MainActivity",
                    //消息内容，必须为Map<String,String>类型
                    mapOf("key" to "从另一个组件过来的MessageBus消息"),
                    //true为即时消息，false只有当接收器的页面处于前台才接收消息
                    false
                )
            }, Modifier.padding(top = 10.dp)) {
                Text(text = "发送消息")
            }
        }
    }
}
